<?php

namespace Iut\omgl6Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Iut\omgl6Bundle\Entity\Emetteur;
use Iut\omgl6Bundle\Form\EmetteurType;

/**
 * Emetteur controller.
 *
 */
class EmetteurController extends Controller
{
    /**
     * Lists all Emetteur entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('Iutomgl6Bundle:Emetteur')->findAll();

        return $this->render('Iutomgl6Bundle:Emetteur:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a Emetteur entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('Iutomgl6Bundle:Emetteur')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Emetteur entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('Iutomgl6Bundle:Emetteur:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),

        ));
    }

    /**
     * Displays a form to create a new Emetteur entity.
     *
     */
    public function newAction()
    {
        $entity = new Emetteur();
        $form   = $this->createForm(new EmetteurType(), $entity);

        return $this->render('Iutomgl6Bundle:Emetteur:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new Emetteur entity.
     *
     */
    public function createAction()
    {
        $entity  = new Emetteur();
        $request = $this->getRequest();
        $form    = $this->createForm(new EmetteurType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();
            $this->createUser($entity);

            return $this->redirect($this->generateUrl('emetteur_show', array('id' => $entity->getId())));
            
        }

        return $this->render('Iutomgl6Bundle:Emetteur:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Displays a form to edit an existing Emetteur entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('Iutomgl6Bundle:Emetteur')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Emetteur entity.');
        }

        $editForm = $this->createForm(new EmetteurType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('Iutomgl6Bundle:Emetteur:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Emetteur entity.
     *
     */
    public function updateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('Iutomgl6Bundle:Emetteur')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Emetteur entity.');
        }

        $editForm   = $this->createForm(new EmetteurType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('emetteur_edit', array('id' => $id)));
        }

        return $this->render('Iutomgl6Bundle:Emetteur:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Emetteur entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('Iutomgl6Bundle:Emetteur')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Emetteur entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('emetteur'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    //N'est plus utilisée car on laisse le choix du mot de passe à la création
    public function generatePass()
	{
		$nbChar = 7;
	
		$characters = '023456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ';
	
		$pass = substr(str_shuffle($characters), 0, $nbChar);
	
		return str_shuffle($pass);
	}
        
    //création d'un profil utilisateur avec les données de l'emetteur
    public function createUser($entity)
	{
		$role = "ROLE_USER";
		 
		$login = $entity->getLoginEmetteur();
		 
		$pass = $entity->getPasswordEmetteur();
		 
		$email = $entity->getMailEmetteur();
		 
		 
		$util=$this->get('fos_user.util.user_manipulator')->create($login,$pass,$email,true,false);
			
		//send an email to the mail's user with login and password
		$message = $login."\n".$pass;
			
		ini_set('SMTP','smtp.univ-savoie.fr');
		mail($email,"Your account on our website",$message);
			
			
		$util->addRole($role);
		$em = $this->getDoctrine()->getEntityManager();
		$em->persist($entity);
		$em->flush();
	}
}
